package com.moon.game.command.guest.lair;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.moon.game.command.BaseCmd;
import com.moon.game.command.RequestSession;
import com.moon.game.constants.Constants;
import com.moon.game.constants.ErrorCode;
import com.moon.game.exception.GException;
import com.moon.game.manager.AnimalChildMG;
import com.moon.game.manager.AnimalMG;
import com.moon.game.manager.ItemMG;
import com.moon.game.resource.model.Animal;
import com.moon.game.resource.model.AnimalChild;
import com.moon.game.resource.model.Item;
import com.moon.game.service.ItemService;
import com.moon.game.service.LairService;
import com.moon.game.service.PlayerService;
import com.moon.game.util.GDateUtils;
import com.moon.game.vos.ErrorMsg;
import com.moon.game.vos.PlayerVo;
import com.moon.game.vos.PlayerVo.Pet;
import com.moon.game.vos.PlayerVo.Tower;

/**
 * @description 孵蛋
 * @author Ranger 作者 E-mail:haozi000005@163.com
 * @version 创建时间：2012-7-31 下午4:57:26
 */
public class IncubationCmd extends BaseCmd {
	private static final Logger log = Logger.getLogger(IncubationCmd.class);
	@Override
	protected void runOnce(RequestSession req) throws GException {
		switch (req.getStatus()) {
		case STATU_INIT:
			doInit(req);
			break;
		case STATU_CHECK:
			doCheckSelf(req);
			break;
		case STATU_RETURN:
			doResponse(req);
			break;
		default:
			break;
		}
	}

	@Override
	protected void doResponse(RequestSession req) throws GException {
		String[] ps = req.getClientRequest().getPs();
		if (ps == null) {
			doResponsePsError(req);
			return;
		}
		PlayerVo player = req.getPlayer();
		List<Tower> towers = player.getTowerList();
		if (towers == null) {
			doResponseErrorMsg(req, ErrorMsg.build(ErrorCode.ERR_NO_TOWER, "进入孵化塔出错，你当前没有孵化塔！"));
			return;
		}
		Integer towerId = Integer.parseInt(ps[0]);
		if (towerId == null) {
			doResponsePsError(req);
			return;
		}
		Tower theTower = LairService.findLair(towers, towerId);
		//没有指定孵化塔
		if (theTower == null) {
			doResponseErrorMsg(req, ErrorMsg.build(ErrorCode.ERR_NO_TOWER, "你当前没有指定的孵化塔！"));
			return;
		}
		
		Animal animal = AnimalMG.instance.getAnimal(theTower.getAnimalId());
		if (animal == null) {
			log.error("can't find the animal:" + theTower.getAnimalId());
			doResponseErrorMsg(req, ErrorMsg.build(ErrorCode.ERR_RESOURCE, "珍兽资源库中找不到指定珍兽！"));
			return;
		}
		
		AnimalChild child = AnimalChildMG.instance.getAnimalChild(animal.getChildId());
		if (child == null) {
			log.error("can't find the child:" + animal.getChildId());
			doResponseErrorMsg(req, ErrorMsg.build(ErrorCode.ERR_RESOURCE, "珍兽孵化资源库中找不到指定孵化兽！"));
			return;
		}
		
		//检测孵化需要消耗的耐久
		int erode = LairService.calcMinusLairEndure(1, 0);
		if (theTower.getCurrEndure() < 2 * erode) {
			//孵化塔持久不够孵化
			req.setVO("tower_id", towerId);
			doResponseErrorMsg(req, ErrorMsg.build(ErrorCode.ERR_LAIR_ENDURE, "你的孵化塔持久太低了，请先修复再进行孵化！"));
			return;
		}
		
		//人物等级必须要高于要孵化的珍兽等级才能进行孵化
		if (player.getLevel() < child.getLevel()) {
			doResponseErrorMsg(req, ErrorMsg.build(ErrorCode.ERR_LACK_LEVEL, "等级低于需要孵化的珍兽等级不能进行孵化，要孵化" + child.getName() + "需要等级" + child.getLevel() + "！"));
			return;
		}
		//检测宠物列表长度
		List<Pet> petList = player.getPetList();
		if (petList == null) {
			petList = new ArrayList<Pet>();
		}
		if (petList.size() >= player.getPetYardCapacity()) {
			doResponseErrorMsg(req, ErrorMsg.build(ErrorCode.ERR_MAX_PET_YARD_CAPACITY, "你的宠物院容量已经达到饱和，不能再容纳更多的宠物了！"));
			return;
		}
		
		//100碎银孵化一次
		boolean minusStatu = PlayerService.checkMoney(req.getSid(), req.getPlayerId(), Constants.INCUBATION_PRICE);
		if (!minusStatu) {
			doResponseErrorMsg(req,ErrorMsg.build(ErrorCode.ERR_NO_MONEY, "你的碎银不足" + Constants.INCUBATION_PRICE + "，不能进行珍兽孵化！"));
			return;
		}
		
		Item item = ItemMG.instance.getItem(animal.getEggId());
		if (item == null) {
			log.error("can't find the egg:" + animal.getEggId());
			doResponseErrorMsg(req, ErrorMsg.build(ErrorCode.ERR_RESOURCE, "道具资源库中找不到指定道具！"));
			return;
		}
		
		int ret = ItemService.delItem(player, animal.getEggId(), 1);
		
		StringBuffer info = new StringBuffer();
		if (ret == Constants.DEL_ITEM_SUCC) {
			log.debug("del " + item.getName() + "succ!");
			//50碎银孵化一次
			minusStatu = PlayerService.minusMoney(req.getSid(), req.getPlayerId(), Constants.INCUBATION_PRICE);
			if (!minusStatu) {
				doResponseErrorMsg(req,ErrorMsg.build(ErrorCode.ERR_NO_MONEY, "你的碎银不足" + Constants.INCUBATION_PRICE + "，不能进行珍兽孵化！"));
				return;
			}
			log.debug("minuse " + Constants.INCUBATION_PRICE + "money succ!");
			//扣除蛋成功
			petList.add(AnimalChildMG.instance.petFactory(animal.getChildId()));
			player.setPetList(petList);
			log.debug("add pet:" + animal.getChildId() + " succ!");
			//减持久
			theTower.setCurrEndure(theTower.getCurrEndure() - erode);
			//成功孵化次数加1
			theTower.setIncubationTimes(theTower.getIncubationTimes() + 1);
			PlayerService.doUpdatePlayer(player.getSid(), player.getId(), player);
			info.append("孵化" + AnimalChildMG.instance.getAnimalChild(animal.getChildId()).getName() + "成功！");
			req.setVO(TAG_INFO, info.toString());
		}else if (ret == Constants.DEL_ITEM_FAIL) {
			doResponseErrorMsg(req, ErrorMsg.build(ErrorCode.ERR_ITEM_BASE, "行囊检测" + item.getName() + "异常！"));
			return;
		}else if (ret == Constants.DEL_ITEM_NOT_ENOUGH) {
			doResponseErrorMsg(req, ErrorMsg.build(ErrorCode.ERR_LACK_THE_ITEM, "您的" + item.getName() + "数量不够！"));
			return;
		}else if (ret == Constants.DEL_ITEM_LACK) {
			doResponseErrorMsg(req, ErrorMsg.build(ErrorCode.ERR_HAVE_NO_THE_ITEM, "您当前没有" + item.getName() + "！"));
			return;
		}
		theTower.setLastShowTime(GDateUtils.now());
		response(req, req.getClientResponse());
	}

}
